using System.ComponentModel.DataAnnotations;

namespace Uantek.Device.Communication.Configuration
{
    /// <summary>
    /// 设备通信模块配置
    /// </summary>
    public class DeviceCommunicationConfig
    {
        /// <summary>
        /// 是否启用设备通信模块
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 设备通信配置
        /// </summary>
        public DeviceSettings Device { get; set; } = new DeviceSettings();

        /// <summary>
        /// 数据收集配置
        /// </summary>
        public DataCollectionSettings DataCollection { get; set; } = new DataCollectionSettings();

        /// <summary>
        /// MQTT发布配置
        /// </summary>
        public MqttSettings Mqtt { get; set; } = new MqttSettings();

        /// <summary>
        /// Redis缓存配置
        /// </summary>
        public RedisSettings Redis { get; set; } = new RedisSettings();

        /// <summary>
        /// 任务调度配置
        /// </summary>
        public JobSchedulingSettings JobScheduling { get; set; } = new JobSchedulingSettings();

        /// <summary>
        /// 日志配置
        /// </summary>
        public LoggingSettings Logging { get; set; } = new LoggingSettings();

        /// <summary>
        /// 性能配置
        /// </summary>
        public PerformanceSettings Performance { get; set; } = new PerformanceSettings();
    }

    /// <summary>
    /// 设备设置
    /// </summary>
    public class DeviceSettings
    {
        /// <summary>
        /// 默认连接超时时间（毫秒）
        /// </summary>
        [Range(1000, 60000)]
        public int DefaultConnectionTimeout { get; set; } = 10000;

        /// <summary>
        /// 默认读取超时时间（毫秒）
        /// </summary>
        [Range(1000, 30000)]
        public int DefaultReadTimeout { get; set; } = 5000;

        /// <summary>
        /// 默认写入超时时间（毫秒）
        /// </summary>
        [Range(1000, 30000)]
        public int DefaultWriteTimeout { get; set; } = 5000;

        /// <summary>
        /// 最大重试次数
        /// </summary>
        [Range(0, 10)]
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 重试间隔（毫秒）
        /// </summary>
        [Range(100, 10000)]
        public int RetryInterval { get; set; } = 1000;

        /// <summary>
        /// 是否启用自动重连
        /// </summary>
        public bool EnableAutoReconnect { get; set; } = true;

        /// <summary>
        /// 自动重连间隔（毫秒）
        /// </summary>
        [Range(5000, 300000)]
        public int AutoReconnectInterval { get; set; } = 30000;

        /// <summary>
        /// 支持的协议列表
        /// </summary>
        public string[] SupportedProtocols { get; set; } = new[] { "Modbus", "OpcUa", "TCP", "Serial" };

        /// <summary>
        /// 设备状态检查间隔（毫秒）
        /// </summary>
        [Range(1000, 600000)]
        public int StatusCheckInterval { get; set; } = 60000;
    }

    /// <summary>
    /// 数据收集设置
    /// </summary>
    public class DataCollectionSettings
    {
        /// <summary>
        /// 是否启用数据收集
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 默认收集间隔（毫秒）
        /// </summary>
        [Range(100, 3600000)]
        public int DefaultInterval { get; set; } = 5000;

        /// <summary>
        /// 批处理大小
        /// </summary>
        [Range(1, 10000)]
        public int BatchSize { get; set; } = 100;

        /// <summary>
        /// 缓冲区大小
        /// </summary>
        [Range(100, 100000)]
        public int BufferSize { get; set; } = 1000;

        /// <summary>
        /// 数据质量阈值（0-100）
        /// </summary>
        [Range(0, 100)]
        public int QualityThreshold { get; set; } = 80;

        /// <summary>
        /// 是否启用数据压缩
        /// </summary>
        public bool EnableCompression { get; set; } = true;

        /// <summary>
        /// 是否启用数据验证
        /// </summary>
        public bool EnableValidation { get; set; } = true;

        /// <summary>
        /// 数据保留天数
        /// </summary>
        [Range(1, 3650)]
        public int RetentionDays { get; set; } = 90;

        /// <summary>
        /// 是否启用实时数据推送
        /// </summary>
        public bool EnableRealTimePush { get; set; } = true;
    }

    /// <summary>
    /// MQTT设置
    /// </summary>
    public class MqttSettings
    {
        /// <summary>
        /// 是否启用MQTT发布
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// MQTT服务器地址
        /// </summary>
        [Required]
        public string ServerAddress { get; set; } = "localhost";

        /// <summary>
        /// MQTT服务器端口
        /// </summary>
        [Range(1, 65535)]
        public int Port { get; set; } = 1883;

        /// <summary>
        /// 客户端ID前缀
        /// </summary>
        public string ClientIdPrefix { get; set; } = "DeviceCommunication";

        /// <summary>
        /// 用户名
        /// </summary>
        public string? Username { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string? Password { get; set; }

        /// <summary>
        /// 默认主题前缀
        /// </summary>
        public string DefaultTopicPrefix { get; set; } = "factory";

        /// <summary>
        /// 服务质量等级（0、1、2）
        /// </summary>
        [Range(0, 2)]
        public int QosLevel { get; set; } = 1;

        /// <summary>
        /// 保持连接间隔（秒）
        /// </summary>
        [Range(10, 3600)]
        public int KeepAliveInterval { get; set; } = 60;

        /// <summary>
        /// 连接超时时间（秒）
        /// </summary>
        [Range(5, 300)]
        public int ConnectionTimeout { get; set; } = 30;

        /// <summary>
        /// 是否启用SSL
        /// </summary>
        public bool EnableSsl { get; set; } = false;

        /// <summary>
        /// 是否启用自动重连
        /// </summary>
        public bool EnableAutoReconnect { get; set; } = true;

        /// <summary>
        /// 自动重连延迟（秒）
        /// </summary>
        [Range(1, 300)]
        public int AutoReconnectDelay { get; set; } = 5;

        /// <summary>
        /// 批量发布大小
        /// </summary>
        [Range(1, 1000)]
        public int BatchPublishSize { get; set; } = 50;
    }

    /// <summary>
    /// Redis设置
    /// </summary>
    public class RedisSettings
    {
        /// <summary>
        /// Redis连接字符串
        /// </summary>
        public string ConnectionString { get; set; } = "localhost:6379";

        /// <summary>
        /// 数据库编号
        /// </summary>
        [Range(0, 15)]
        public int Database { get; set; } = 0;

        /// <summary>
        /// 键前缀
        /// </summary>
        public string KeyPrefix { get; set; } = "device_comm:";

        /// <summary>
        /// 默认过期时间（秒）
        /// </summary>
        [Range(60, 31536000)]
        public int DefaultExpiration { get; set; } = 86400; // 24小时

        /// <summary>
        /// 连接池大小
        /// </summary>
        [Range(1, 100)]
        public int PoolSize { get; set; } = 10;

        /// <summary>
        /// 命令超时时间（毫秒）
        /// </summary>
        [Range(1000, 60000)]
        public int CommandTimeout { get; set; } = 5000;
    }

    /// <summary>
    /// 任务调度设置
    /// </summary>
    public class JobSchedulingSettings
    {
        /// <summary>
        /// 是否启用任务调度
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 设备通信任务配置
        /// </summary>
        public JobConfig DeviceCommunicationJob { get; set; } = new JobConfig
        {
            Enabled = true,
            CronExpression = "0 */1 * * * ?", // 每分钟执行一次
            MaxConcurrency = 1
        };

        /// <summary>
        /// 数据持久化任务配置
        /// </summary>
        public JobConfig DataPersistenceJob { get; set; } = new JobConfig
        {
            Enabled = true,
            CronExpression = "0 0 2 * * ?", // 每天凌晨2点执行
            MaxConcurrency = 1
        };

        /// <summary>
        /// 任务执行超时时间（分钟）
        /// </summary>
        [Range(1, 1440)]
        public int JobExecutionTimeout { get; set; } = 30;

        /// <summary>
        /// 最大并发任务数
        /// </summary>
        [Range(1, 50)]
        public int MaxConcurrentJobs { get; set; } = 5;
    }

    /// <summary>
    /// 任务配置
    /// </summary>
    public class JobConfig
    {
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// Cron表达式
        /// </summary>
        [Required]
        public string CronExpression { get; set; } = string.Empty;

        /// <summary>
        /// 最大并发数
        /// </summary>
        [Range(1, 10)]
        public int MaxConcurrency { get; set; } = 1;

        /// <summary>
        /// 任务超时时间（分钟）
        /// </summary>
        [Range(1, 1440)]
        public int TimeoutMinutes { get; set; } = 30;

        /// <summary>
        /// 是否允许任务重叠执行
        /// </summary>
        public bool AllowConcurrentExecution { get; set; } = false;
    }

    /// <summary>
    /// 日志设置
    /// </summary>
    public class LoggingSettings
    {
        /// <summary>
        /// 日志级别
        /// </summary>
        public string Level { get; set; } = "Information";

        /// <summary>
        /// 是否启用数据日志
        /// </summary>
        public bool EnableDataLogging { get; set; } = true;

        /// <summary>
        /// 是否启用性能日志
        /// </summary>
        public bool EnablePerformanceLogging { get; set; } = true;

        /// <summary>
        /// 日志保留天数
        /// </summary>
        [Range(1, 365)]
        public int RetentionDays { get; set; } = 30;

        /// <summary>
        /// 日志文件大小限制（MB）
        /// </summary>
        [Range(1, 1024)]
        public int MaxFileSizeMB { get; set; } = 100;
    }

    /// <summary>
    /// 性能设置
    /// </summary>
    public class PerformanceSettings
    {
        /// <summary>
        /// 最大并发连接数
        /// </summary>
        [Range(1, 1000)]
        public int MaxConcurrentConnections { get; set; } = 100;

        /// <summary>
        /// 线程池大小
        /// </summary>
        [Range(1, 100)]
        public int ThreadPoolSize { get; set; } = 10;

        /// <summary>
        /// 内存缓存大小（MB）
        /// </summary>
        [Range(10, 1024)]
        public int MemoryCacheSizeMB { get; set; } = 100;

        /// <summary>
        /// 是否启用性能监控
        /// </summary>
        public bool EnablePerformanceMonitoring { get; set; } = true;

        /// <summary>
        /// 性能数据收集间隔（秒）
        /// </summary>
        [Range(10, 3600)]
        public int PerformanceDataInterval { get; set; } = 60;

        /// <summary>
        /// GC优化设置
        /// </summary>
        public bool EnableGCOptimization { get; set; } = true;
    }
}